package com.zyk.leetcode;

import java.util.HashSet;

/**
 * @author zhangsan
 * @date 2021/4/7 14:06
 */
public class M0101 {

    // https://leetcode-cn.com/problems/is-unique-lcci/
    public static boolean isUnique(String astr) {
        char[] str = astr.toCharArray();
        HashSet<Character> set = new HashSet<>();
        for (char c : str) {
            if(set.contains(c)) {
                return false;
            }
            set.add(c);
        }
        return true;
    }

    // 使用辅助数组
    public static boolean isUnique2(String astr) {
        boolean[] help = new boolean[26];
        char[] str = astr.toCharArray();
        int path;
        for (char c : str) {
            path = c-'a';
            if(help[path]) {
                return false;
            }
            help[path] = true;
        }
        return true;
    }
    // 使用一个32位的int变量做辅助
    public static boolean isUnique3(String astr) {
        int help = 0;
        for (int i = 0; i < astr.length(); i++) {
            int path = astr.charAt(i)-'a';
            if( (help & (1<<path)) != 0) {
                return false;
            }
            help |= (1<<path);
        }
        return true;
    }


    // for test
    public static void main(String[] args) {
//        System.out.println((int) 'a');
        String s = "leetcode";
        System.out.println(isUnique(s));
        System.out.println(isUnique2(s));
        System.out.println(isUnique3(s));
    }


}
